package com.dao;

import java.util.List;

import com.beans.MemberInfo;
import com.jdbc.DBUtil;
import com.utils.PageInfo;
import com.utils.StrUtil;

public class MemberDao {
	
	//根据会员id删除会员
	public int delMemberById(int id){
		String sql="delete from memberInfo where id=?";
		return DBUtil.update(sql, id);
		
	}
	//根据会员id查询会员信息
	public MemberInfo getMemeberById(int id){
		String sql="select * from memberInfo where id=? "; 
		return DBUtil.getOneObject(sql, MemberInfo.class, id);
	}
	/**
	 * @param memberNo 会员账号
	 * @param beginDate 会员注册起始日期
	 * @param endDate 会员注册结束日期
	 * @param pageInfo 分页信息
	 * @return 会员列表
	 */
	public List<MemberInfo> getMemberList(String memberNo, String beginDate,String endDate, PageInfo pageInfo) {
		String sql="select * from memberInfo where 1=1 "; 
		
		if(!StrUtil.isNullOrEmpty(memberNo)){
			sql+=" and memberNo='"+memberNo+"' ";
		}
		if(!StrUtil.isNullOrEmpty(beginDate)){
			sql+=" and registerDate>='"+beginDate+"' ";
		}
		if(!StrUtil.isNullOrEmpty(endDate)){
			sql+=" and registerDate <='"+endDate+" 23:59:59' ";
		}
		
		sql+=" order by registerDate desc limit ? , ? "; 
		
		return DBUtil.getList(sql, MemberInfo.class,pageInfo.getBeginRow(),pageInfo.getPageSize());
	}

	//多条件查询会员数量
	public int getMemberCount(String memberNo, String beginDate, String endDate) {
		String sql="select count(*) from memberInfo where 1=1 "; 
		
		if(!StrUtil.isNullOrEmpty(memberNo)){
			sql+=" and memberNo='"+memberNo+"' ";
		}
		if(!StrUtil.isNullOrEmpty(beginDate)){
			sql+=" and registerDate>='"+beginDate+"' ";
		}
		if(!StrUtil.isNullOrEmpty(endDate)){
			sql+=" and registerDate <='"+endDate+" 23:59:59' ";  
		}

		Long  count= DBUtil.getScalar(sql);
		
		return new Integer(count+""); 
	}
}
